<template>
  <div class="app-container news-container">
    <el-form size="mini" :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item label="查询：">
        <el-input clearable placeholder="输入任务名称" style="width: 300px" v-model="searchForm.name"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="sts">
        <el-select placeholder="全部" style="width: 120px" v-model="searchForm.label">
          <el-option :key="item.value" :label="item.label" :value="item.value" v-for="item in sts_lis"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="getDataList()" icon="el-icon-search" type="primary">查询</el-button>
        <el-button size="mini" @click="refDataList()" icon="el-icon-refresh" type="primary">重置</el-button>
        <el-button size="mini" @click="addOrUpdateHandle()" type="primary" v-if="Jurisdictionlist.indexOf('Add') > -1">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" size="mini" :height="tableHeight" border ref="table" style="width: 100%" v-loading="dataListLoading">
      <el-table-column align="center" header-align="center" label="序号" prop="typeName" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="名称" prop="name"></el-table-column>
      <el-table-column align="center" header-align="center" label="执行类型" prop="label">
        <template slot-scope="scope">
          {{ scope.row.label === '1' ? '定时任务' : '实时任务' }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="运行状态" prop="run_sts">
        <template slot-scope="scope">
          {{ scope.row.run_sts === '1' ? '停止' : '运行' }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="创建人" prop="created_user"></el-table-column>
      <el-table-column align="center" header-align="center" label="创建时间" prop="created_time"></el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="320">
        <template slot-scope="scope">
          <el-button size="mini" @click="lookHandelBtn(scope.row)" plain type="primary">详情</el-button>
          <el-button size="mini" @click="addOrUpdateHandle(scope.row)" plain type="primary" v-if="Jurisdictionlist.indexOf('Edit') > -1">编辑</el-button>
          <el-button size="mini" @click="handleDel(scope.row)" plain type="danger" v-if="Jurisdictionlist.indexOf('Delete') > -1">删除</el-button>
          <el-button size="mini" @click="handleOperate(scope.row, 1)" plain type="success" v-if="Jurisdictionlist.indexOf('Delete') > -1">执行</el-button>
          <el-button size="mini" @click="handleOperate(scope.row, 2)" plain type="danger" v-if="Jurisdictionlist.indexOf('Delete') > -1">停止</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination small :current-page="pageIndex" :page-size="pageSize" :page-sizes="[10, 20, 50, 100]" :total="totalPage" @current-change="currentChangeHandle" @size-change="sizeChangeHandle" layout="total, sizes, prev, pager, next, jumper" style="margin-top: 10px"></el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update @refreshList="getDataList" ref="addOrUpdate"></add-or-update>
    <!-- 信息详情弹框 -->
    <el-dialog :visible.sync="dialogSendInfo" title="资讯详情">
      <div>
        <div class="info_title">{{ current.title }}</div>
        <div class="info_time">{{ current.created_time }}</div>
        <div class="info_content" v-html="current.content"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './add-or-update'
import { mapGetters } from 'vuex'
import { ArrTaskApi, DeleteTaskApi, RunScriptApi, StopScriptApi } from '@/api/project'
var app

export default {
  name: 'SerClassManage',
  computed: {
    ...mapGetters(['name']),
  },
  beforeCreate() {
    app = this
  },
  filters: {},
  data() {
    return {
      dialogSendInfo: false,
      current: {},
      allClassArr: [],
      classArrFuzi: [],
      classArr: [],
      Jurisdictionlist: [], // 权限列表
      searchForm: {
        label: '',
        name: '',
        industry: '',
      },
      dialogTableVisible: false,
      dataList: [],
      tableHeight: 50,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      areaIdArr: [],
      mainBodyArr: [],
      addOrUpdateVisible: false,
      sts_lis: [
        { value: '1', label: '启动' },
        { value: '2', label: '停止' },
        { value: '3', label: '运行' },
      ],
    }
  },
  components: { AddOrUpdate },
  mounted() {
    this.getDataList()
    this.Jurisdictionlist = JSON.parse(localStorage.getItem('jurisdiction'))[this.$route.path]
    this.$nextTick(function () {
      this.tableHeight = window.innerHeight - 165 - 65
      // 监听窗口大小变化
      let self = this
      window.onresize = function () {
        self.tableHeight = window.innerHeight - 165 - 65
      }
    })
  },
  methods: {
    handleOperate(row, label) {
      this.$confirm('确认执行此脚本?', '提示', {
        type: 'warning',
      })
        .then(() => {
          if (label === 1) {
            RunScriptApi({ id: row.id }).then((res) => {
              if (res.code === 200) {
                this.$message.success('运行成功')
                this.getDataList()
              } else {
                this.$message.error(res.result)
              }
            })
          } else {
            StopScriptApi({ id: row.id }).then((res) => {
              if (res.code === 200) {
                this.$message.success('操作成功')
                this.getDataList()
              } else {
                this.$message.error(res.result)
              }
            })
          }
        })
        .catch(() => {})
    },
    handleChange() {},
    lookHandelBtn(row) {
      // 查看详情
      this.dialogSendInfo = true
      this.current = row
    },
    //重置
    refDataList() {
      this.searchForm = {
        title: '',
        label: '',
      }
      this.getDataList()
    },
    //重置
    refInfo() {
      this.searchForm.title = ''
      this.getDataList()
    },
    handleDel(row) {
      this.$confirm('确认删除吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          let para = {
            ids: [row.id],
          }
          DeleteTaskApi(para).then((res) => {
            if (res.code === 200) {
              this.$message.success('删除成功')
              this.getDataList()
            }
          })
        })
        .catch(() => {})
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.searchForm.date = this.searchForm.date || []
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchForm)
      ArrTaskApi(info)
        .then((res) => {
          this.dataList = res.result.items
          this.totalPage = res.result.total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {},
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.$refs.addOrUpdate.open(row, this.roleOptions)
    },
  },
}
</script>

<style>
.info_content img {
  max-width: 100%;
  height: auto;
}
</style>

<style lang="scss" scoped>
.info_title {
  font-size: 26px;
  font-weight: bolder;
  margin-bottom: 10px;
}
.fl {
  float: left;
}

.fr {
  float: right;
}

.clearfix::after {
  content: '';
  display: block;
  clear: both;
}

.thiscontainer {
}
</style>
